package com.hmyd.hmyd_android.ui;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.VerifyCode;
import com.hmyd.hmyd_android.utils.MyToast;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 重置安全问题
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_reset_question)
public class ResetQuestionActivity extends BasicActivity implements OnClickListener {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.phone)
	private EditText mEdPhone;
	@ViewInject(R.id.et_code)
	private EditText mEdCode;

	@ViewInject(R.id.btn_get_code)
	private Button mBtnGetCode;
	@ViewInject(R.id.next)
	private Button mBtnNext;

	private String mCodeId;

	private final int SEND_REGISTER_VERIFY_CODE = 1;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_get_code:
			getCode();
			break;
		case R.id.next:
			next();
			break;
		default:
			break;
		}

	}

	private void next() {
		String code = null;
		code = mEdCode.getText().toString().trim();
		// startActivity(ResetSecurityIssuesActivity.getIntent(
		// ResetQuestionActivity.this, mCode, codeId));
		if (TextUtils.isEmpty(code)) {
			MyToast.showHMToast(ResetQuestionActivity.this, false, "请输入验证码");
			return;
		}

		Intent intent = new Intent();
		intent.putExtra("code", code);
		intent.putExtra("codeId", mCodeId);
		intent.setClass(ResetQuestionActivity.this, ResetSecurityIssuesActivity.class);
		startActivity(intent);
	}

	private void getCode() {
		onRequest(SEND_REGISTER_VERIFY_CODE, UrlConstant.SENDSAFECENTERVERIFYCODE, HttpMethod.GET,
				null);
		CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				mBtnGetCode.setText("剩余" + millisUntilFinished / 1000 + "秒");
				mBtnGetCode.setEnabled(false);
			}

			@Override
			public void onFinish() {
				mBtnGetCode.setText("获取验证码");
				mBtnGetCode.setEnabled(true);
			}
		}.start();

	}

	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		switch (postId) {
		case SEND_REGISTER_VERIFY_CODE:
			java.lang.reflect.Type type = new TypeToken<VerifyCode>() {
			}.getType();
			VerifyCode verifyCode = gson.fromJson(result, type);
			mCodeId = verifyCode.codeId;
			mBtnNext.setEnabled(true);
			break;
		default:
			break;

		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText("问题重置");
		home_title.setOnTouchListener(touch);

		mBtnGetCode.setOnClickListener(this);
		mBtnNext.setOnClickListener(this);

		mEdPhone.setText(AppBasic.tel);
		mEdPhone.setFocusable(false);

		if (!TextUtils.isEmpty(AppBasic.tel)) {
			mBtnGetCode.setEnabled(true);
		}

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onPostFailure(int postId, String msg) {
		// TODO Auto-generated method stub

	}

}
